import 'package:capsule/enums/file_filter_enum.dart';
import 'package:capsule/widgets/dialog/dialog_template.dart';
import 'package:capsule/widgets/form/radio_option.dart';
import 'package:flutter/material.dart';

import '../../utils/file_utils.dart';
import '../form/filter_group.dart';

class FileSelectorDialog extends StatefulWidget {
  final void Function()? onCancel;
  final void Function(String folder, FileFilterEnum filter)? onOk;
  const FileSelectorDialog({super.key, this.onOk, this.onCancel});
  @override
  State<FileSelectorDialog> createState() => _FileSelectorDialogState();
}

class _FileSelectorDialogState extends State<FileSelectorDialog> {
  late final TextEditingController _currentFolderController;
  FileFilterEnum _currentFilter = FileFilterEnum.book;
  @override
  void initState() {
    super.initState();

    _currentFolderController = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    double dlgWidth = 560;
    return DialogTemplate(
      child: Column(
        children: [
          SizedBox(
            width: dlgWidth - 40,
            child: FilterGroup(
              currentFilter: _currentFilter,
              onChanged: (value) {
                _currentFilter = value!;
              },
            ),
          ),
          const SizedBox(
            height: 10,
          ),
          Row(
            children: [
              SizedBox(
                width: dlgWidth - 90,
                child: TextFormField(
                  controller: _currentFolderController,
                ),
              ),
              const SizedBox(
                width: 10,
              ),
              IconButton(
                  onPressed: () async {
                    String? selectedFolder = await showFolderSelector(context,
                        allowedExtensions: _currentFilter.extensions);

                    if (selectedFolder != null) {
                      if (mounted) {
                        _currentFolderController.value =
                            TextEditingValue(text: selectedFolder);
                      }
                    }
                  },
                  icon: const Icon(Icons.folder)),
            ],
          ),
        ],
      ),
      onOk: () {
        widget.onOk?.call(_currentFolderController.value.text, _currentFilter);
      },
    );
  }
}
